<?php

namespace app\controller\admin\cnf;

use app\bll\Site as BllSite;
use app\bll\SiteAdmin as BllSiteAdmin;
use app\bll\User as BllUser;
use app\bll\UserField;
use core\controller\AdminBase;
use Doctrine\DBAL\Types\Types;
use Exception;
use Illuminate\Database\Schema\Blueprint;
use support\Db;
use support\View;

class Site extends AdminBase
{
    public function index()
    {

        $bllSite = new BllSite();
        $sites = $bllSite->paginator();
        View::assign('sites', $sites);
        return $this->view();
    }


    public function add()
    {
        if (is_post()) {
            try {
                $bllSite = new BllSite();
                $bllSite->add(request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view();
        }
    }

    public function edit()
    {

        $id = request()->get('id');
        $bllSite = new BllSite();
        if (is_post()) {
            try {
                $bllSite = new BllSite();
                $bllSite->edit($id, request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {

            $siteinfo = $bllSite->find($id);
            View::assign('siteinfo', $siteinfo);
            return $this->view();
        }
    }


    public function setStatus()
    {
        try {
            $bllSite = new BllSite();
            $bllSite->setStatus(
                request()->get('id'),
                request()->post('status')
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }

    /**
     * 超管
     *
     * @return mixed
     */
    public function systemAdmin()
    {
        if (is_post()) {
            try {

                $data = request()->post();
                $data['issuperadmin'] = 1;
                $bllSiteAdmin = new BllSiteAdmin();
                $bllSiteAdmin->add($data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {


            $type = request()->get('type', '');
            switch ($type) {
                case 'seluser':
                    $bllUser = new BllUser();
                    $users = $bllUser->paginator(
                        username: request()->get('username', ''),
                        phone: request()->get('phone', '')
                    );
                    return $this->success(['users' => $users->items(), 'total' => $users->total()]);
                    break;
                case 'seladmin':
                    $bllSiteAdmin = new BllSiteAdmin();
                    $admins = $bllSiteAdmin->selectBySiteid(request()->get('siteid'), 1);
                    return $this->success($admins);
                    break;
            }
            return $this->view(frame: false);
        }
    }


    /**
     * 移除系统管理员
     *
     * @return mixed
     */
    public function delSystemAdmin()
    {
        $siteid = request()->get('siteid');
        $uid    = request()->get('uid');
        try {
            $bllSiteAdmin = new BllSiteAdmin();
            $bllSiteAdmin->delete($uid, $siteid);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
